<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateNavigateRequest;
use App\Http\Requests\UpdateNavigateRequest;
use App\Models\Navigate;
use App\Repositories\NavigateRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class NavigateController extends AppBaseController
{
    /** @var  NavigateRepository */
    private $navigateRepository;
    private $pic;

    public function __construct(NavigateRepository $navigateRepo, PicturesRepository $picturesRepository)
    {
        $this->navigateRepository = $navigateRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the Navigate.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->navigateRepository->pushCriteria(new RequestCriteria($request));
        $navigates = Navigate::orderBy('sort','asc')->paginate(20);

        return view('navigates.index')
            ->with('navigates', $navigates);
    }

    /**
     * Show the form for creating a new Navigate.
     *
     * @return Response
     */
    public function create()
    {
        return view('navigates.create');
    }

    /**
     * Store a newly created Navigate in storage.
     *
     * @param CreateNavigateRequest $request
     *
     * @return Response
     */
    public function store(CreateNavigateRequest $request)
    {
        $input = $request->all();

        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"nihaocai",null,null);
            $input['image'] = $image->url;
        }
        $navigate = $this->navigateRepository->create($input);

        Flash::success('Navigate saved successfully.');

        return redirect(route('navigates.index'));
    }

    /**
     * Display the specified Navigate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $navigate = $this->navigateRepository->findWithoutFail($id);

        if (empty($navigate)) {
            Flash::error('Navigate not found');

            return redirect(route('navigates.index'));
        }

        return view('navigates.show')->with('navigate', $navigate);
    }

    /**
     * Show the form for editing the specified Navigate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $navigate = $this->navigateRepository->findWithoutFail($id);

        if (empty($navigate)) {
            Flash::error('Navigate not found');

            return redirect(route('navigates.index'));
        }

        return view('navigates.edit')->with('navigate', $navigate);
    }

    /**
     * Update the specified Navigate in storage.
     *
     * @param  int              $id
     * @param UpdateNavigateRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateNavigateRequest $request)
    {
        $navigate = $this->navigateRepository->findWithoutFail($id);

        $input = $request->all();

        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"nihaocai",null,null);
            $input['image'] = $image->url;
        }
        if (empty($navigate)) {
            Flash::error('Navigate not found');

            return redirect(route('navigates.index'));
        }

        $navigate = $this->navigateRepository->update($input, $id);

        Flash::success('Navigate updated successfully.');

        return redirect(route('navigates.index'));
    }

    /**
     * Remove the specified Navigate from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $navigate = $this->navigateRepository->findWithoutFail($id);

        if (empty($navigate)) {
            Flash::error('Navigate not found');

            return redirect(route('navigates.index'));
        }

        $this->navigateRepository->delete($id);

        Flash::success('Navigate deleted successfully.');

        return redirect(route('navigates.index'));
    }
}
